package com.one.group.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.processor.util.StrUtil;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.UpdateMaterialDto;
import com.one.group.model.dto.MaterialQueryDto;
import com.one.group.model.entity.Material;
import com.one.group.result.Result;
import com.one.group.service.MaterialService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("material")
@Tag(name = "物料管理接口")
public class MaterialController {
    @Autowired
    MaterialService materialService;
    @PostMapping("list")
    @Operation(summary = "查询物料列表")
    public Result list(@RequestBody MaterialQueryDto materialQueryDto){
//        1.准备分页条件
        Page<Material> page = new Page<>(materialQueryDto.getPage(), materialQueryDto.getLimit());
//        2.链式编程准备查询条件
        QueryWrapper queryWrapper = new QueryWrapper();

// 2. 构建查询条件
        String name = materialQueryDto.getName();
        if (!StrUtil.isBlank(name)) {
            queryWrapper.where(
                    QueryMethods.column(Material::getName).like(name)
                            .or(QueryMethods.column(Material::getBrand).like(name))
            );
        }
//        3.查询，返回分页数据
        Page<Material> materialPage = materialService.page(page, queryWrapper);
        return ResultUtil.success(materialPage);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询物料信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(materialService.getById(id));
    }
    @GetMapping("findAll")
    @Operation(summary = "查询所有物料信息")
    public Result findAll() {
        return ResultUtil.success(materialService.list());
    }
    @GetMapping("/delete")
    @Operation(summary = "删除物料信息")
    public Result delete(@RequestParam Long id){
        if (materialService.removeById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/add")
    @Operation(summary = "添加物料信息")
    public Result add(@RequestBody UpdateMaterialDto updateMaterialDto){
        Material material = new Material();
        BeanUtils.copyProperties(updateMaterialDto,material);
        if (materialService.save(material)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/update")
    @Operation(summary = "修改物料信息")
    public Result update(@RequestBody UpdateMaterialDto updateMaterialDto){
        Material material = new Material();
        BeanUtils.copyProperties(updateMaterialDto,material);
        if (materialService.updateById(material)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除物料信息")
    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto){
        if (materialService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
